<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('User_model');
        $this->load->model('Usernum_model');
        $this->load->model('cert_model');
    }
	

	public function index($userid)
	{
		$usertype = (int)$this->input->get('t');
		$areaType = $this->input->get('a');
		if (!isset($areaType))
			$areaType = 10;
		if(!$usertype)
			$usertype = 1;
		//浏览
		$sysVisittype = _get_config('visittype');
		$this->load->service('user_service');
		$this->user_service->visit($userid, $this->loginID, $sysVisittype['home']);
		//-浏览

		//$this->load->service('user_service');
		$oUser = $this->user_service->get_user_homeinfo($userid, $this->loginID);
        
        //如果此人是属于经纪公司的，此经纪公司是认证过的，则旗下所有的人都是认证过的
        if(!empty($insid)) {
            $userCert = $this->cert_model->get_by_id($insid);
        } else {
            //认证
            $userCert = $this->cert_model->get_by_id($userid);
        }

		$page     = _get_page();
		$pagesize = 20;
		$arrParam = array();
// 		$arrWhere = array('insid'=>$userid, 'status'=>2,'showimg<>'=>"''",'showimg2<>'=>"''");
		$arrWhere = array('insid'=>$userid, 'status'=>2);
		
		//模特个数
		$where = $arrWhere;
		$where["usertype"] = 1;
		$model_count = $this->User_model->getUserCount($where);
		//摄影师个数
		$where["usertype"] = 4;
		$photo_count = $this->User_model->getUserCount($where);
		//化妆师个数
		$where["usertype"] = 5;
		$makeup_count = $this->User_model->getUserCount($where);
		
		if($usertype)
		{
			$arrParam['t'] = $usertype;
			$arrWhere['usertype'] = $usertype;
		}
		
		//模特地域分类查询条件
		if (isset($areaType))
		{
			if ($areaType != 10)
				$arrWhere['area'] = $areaType;
			$arrParam['a'] = $areaType;
		}
		
		//模特地域分类
		$areaTypeArr = array();
		if ($usertype == 1)
		{
			$where = array('insid'=>$userid, 'status'=>2, 'usertype'=>1);
			$models = $this->User_model->list_by_where($where,'area');
			if ($models)
			{
				$area_list = array();
				$conf_area = _get_config('area_type');
				foreach ($models as $key=>$val)
				{
					if (!in_array($val['area'], $area_list))
					{
						array_push($area_list, $val['area']);
						$areaTypeArr[$val['area']] = $conf_area[$val['area']];
					}
				}
				ksort($areaTypeArr);
			}
		}

		$list = $this->User_model->fetch_page($page, $pagesize, $arrWhere,'id,nickname,showimg,showimg2,city,usertype,company','addtime asc');
// 		echo $this->User_model->db->last_query();die;
		$this->load->model('Userdetail_model');
		
		//模特
		if ($usertype == 1 && $list['rows'])
		{
			//获取模特基本信息
			foreach ($list['rows'] as $key=>$val)
			{
				$userdetail = $this->Userdetail_model->get_info_by_userid($val['id']);
				if ($userdetail)
				{
					$list['rows'][$key]['height'] = $userdetail['height'];
					$list['rows'][$key]['bust'] = $userdetail['bust'];
					$list['rows'][$key]['waist'] = $userdetail['waist'];
					$list['rows'][$key]['hips'] = $userdetail['hips'];
					$list['rows'][$key]['shoes'] = $userdetail['shoes'];
				}
			}
			
			//获取点击量
			$this->load->model('Usernum_model');
			foreach ($list['rows'] as $key => $v) {
                $numInfo = $this->Usernum_model->get_info_by_userid($v['id']);
                $list['rows'][$key]['num_info'] = $numInfo;
            }
		}
		
		//摄影师、化妆师
		if (($usertype == 4 or $usertype == 5) && $list['rows'])
		{
			$typeArr = _get_config('type');
			$insTypeArr = $typeArr[2];
			foreach($list['rows'] as $key=>$ins) {
				// 处理类型展示
				$typeStr = '';
				$infoArr = array();
				$info =  $this->Userdetail_model->get_info_by_userid($ins['id']);
				if (!empty($info['type'])) {
					if (strstr($info['type'], ',')) {
						$infoArr= explode(',', $info['type']);
					} else {
						$typeStr = $insTypeArr[$info['type']];
					}
				}
				
				if ($infoArr) {
					foreach($infoArr as $it) {
						$typeStr .= ','.$insTypeArr[$it];
					}
				}
				if (strstr($typeStr, ','))
				{
					$type_str = ltrim($typeStr, ',');
					$type_arr = explode(',', $type_str);
					$type_arr = array_unique($type_arr);
					$typeStr = implode(',', $type_arr);
				}
				
				$info['type_str'] = $typeStr;
				$list['rows'][$key]['detail_info'] = $info;
				
				//获取点击量
				$numInfo = $this->Usernum_model->get_info_by_userid($ins['id']);
				$list['rows'][$key]['num_info'] = $numInfo;
			}
		}
		
		//用户类型、分类
		$userRole = '';
		if ($oUser['usertype'] == 1)
		{
			$userRole = $oUser['role']?$oUser['role']:'模特';
		}
		elseif ($oUser['usertype'] == 2)
		{
			$oSysType = _get_config('type');
			$oSysType = $oSysType[2];
			$oSysStr = '';
			foreach ($oSysType as $key => $v)
			{
				if(strpos(','.$oUser['type'].',',','.$key.',')>-1)
				{
					if ($oSysStr)
					{
						$oSysStr .= ','.$v;
					}
					else
					{
						$oSysStr = $v;
					}
				}
			}
			$userRole = $oSysStr?$oSysStr:'机构';
		}
		elseif ($oUser['usertype'] == 3)
		{
			$userRole = '公司';
		}
		elseif ($oUser['usertype'] == 4)
		{
			$userRole = $oUser['role']?$oUser['role']:'摄影师';
		}
		elseif ($oUser['usertype'] == 5)
		{
			$userRole = $oUser['role']?$oUser['role']:'化妆师';
		}
		elseif ($oUser['usertype'] == 6)
		{
			if ($oUser['role'])
			{
				$userRole = $oUser['role'];
			}
			else
			{
				if ($oUser['type'])
				{
					$oSysType = _get_config('type');
					$oSysType = $oSysType[6];
					$userRole = $oSysType[$oUser['type']];
				}
				else
				{
					$userRole = '艺人';
				}
			}
		}
		
		//分页
		$pagecfg = array();
		$pagecfg['base_url']     = _create_url('i/model/'.$userid, $arrParam);
		$pagecfg['total_rows']   = $list['count'];
		$pagecfg['cur_page'] = $page;
		$pagecfg['per_page'] = $pagesize;
		//$this->load->library('pagination');
		$this->pagination->initialize($pagecfg);
		$list['pages'] = $this->pagination->create_links();

		$result = array(
			'oUser' => $oUser,
            'oCert' => $userCert,
			'list' => $list,
			'arrParam' => $arrParam,
			'model_count' => $model_count,
			'photo_count' => $photo_count,
			'makeup_count' => $makeup_count,
			'areaType' => $areaTypeArr,
			'userRole' => $userRole
			);
		$this->load->view('i/model',$result);
	}

	public function info($id)
	{
		$this->load->service('user_service');
		$oIns = $this->user_service->get_ins_homeinfo($id, $this->loginID);
		$oInfo = $oIns['modelinfo'];
		unset($oIns['modelinfo']);

		$oBody = array();
		if($oInfo['usertype']==1)
		{
			$this->load->model('Userbody_model');
			$oBody = $this->Userbody_model->get_by_id($id);
		}

		$result = array(
			'oUser' => $oIns,
			'oInfo' => $oInfo,
			'oBody' => $oBody,
			);
		$view = 'i/modelinfo';
		if(in_array($oInfo['usertype'],array(4,5)))
			$view = 'i/modelinfo_photo';
		$this->load->view($view,$result);
	}
	

}